package edu.ccut.saturn.component.loader.node;

/**
 * 用于解析action组件配置文件中“网页上下文”结点的加载类。
 * <p>继承了<code>NodeInfoLoader</code>,并实现了<code>loadNode</code>方法。<p>
 * 
 * @author 温建
 * @version 1.0 2008.8.18
 * 
 * @see <code>WebContextNodeInfo</code>
 */
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;
import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.INodeInfo;
import edu.ccut.saturn.component.IWebContextValueInfo;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.impl.WebContextValueInfo;
import edu.ccut.saturn.component.impl.node.WebContextNodeInfo;

public class WebContextNodeInfoLoader extends NodeInfoLoader {

	public static final String KEY_WEBCONTEXT = "webContext";

	public static final String KEY_WEBCONTEXT_VALUEMOVES = "value-moves";

	public static final String KEY_WEBCONTEXT_VALUEMOVE = "value-move";

	public static final String KEY_WEBCONTEXT_VALUEMOVE_ID = "id";

	public static final String KEY_WEBCONTEXT_VALUEMOVE_KEY = "key";

	public static final String KEY_WEBCONTEXT_VALUEMOVE_SCOPE = "scope";

	public static final String KEY_WEBCONTEXT_VALUEMOVE_NEWKEY = "newKey";

	public static final String KEY_WEBCONTEXT_VALUEMOVE_NEWSCOPE = "newScope";

	public static final String KEY_WEBCONTEXT_VALUEMOVE_ISDELETE = "isDelete";

	@SuppressWarnings("unchecked")
	public List<INodeInfo> loadNode(Element root, Bundle bundle)
			throws SaturnComponentException {

		List<Element> webContextElements = root.elements(KEY_WEBCONTEXT);

		List<INodeInfo> webContextNodeInfos = new ArrayList<INodeInfo>(
				webContextElements.size());
		try {
			for (Element webContextElement : webContextElements) {
				String id = webContextElement.attributeValue(KEY_NODE_ID)
						.trim();

				String x = webContextElement.attributeValue(KEY_NODE_X).trim();
				String y = webContextElement.attributeValue(KEY_NODE_Y).trim();
				String nextId = webContextElement.attributeValue(KEY_NODE_NEXT)
						.trim();

				String name = webContextElement.elementText(KEY_NODE_NAME);
				String description = webContextElement
						.elementText(KEY_NODE_DESCRIPTION);

				List<Element> valueMoveElements = webContextElement.element(
						KEY_WEBCONTEXT_VALUEMOVES).elements(
						KEY_WEBCONTEXT_VALUEMOVE);

				WebContextNodeInfo webContextNodeInfo = new WebContextNodeInfo(
						id, name, description, nextId, null, x, y);

				List<IWebContextValueInfo> values = new ArrayList<IWebContextValueInfo>();

				for (Element valueMoveElement : valueMoveElements) {
					String valueId = valueMoveElement.attributeValue(
							KEY_WEBCONTEXT_VALUEMOVE_ID).trim();

					String key = valueMoveElement.attributeValue(
							KEY_WEBCONTEXT_VALUEMOVE_KEY).trim();

					String scope = valueMoveElement.attributeValue(
							KEY_WEBCONTEXT_VALUEMOVE_SCOPE).trim();

					String newKey = valueMoveElement.attributeValue(
							KEY_WEBCONTEXT_VALUEMOVE_NEWKEY).trim();

					String newScope = valueMoveElement.attributeValue(
							KEY_WEBCONTEXT_VALUEMOVE_NEWSCOPE).trim();

					String isDelete = valueMoveElement.attributeValue(
							KEY_WEBCONTEXT_VALUEMOVE_ISDELETE).trim();

					logger.debug("  load webcontext name = \"" + name
							+ "\" value id = \"" + valueId + "\" key = \"" 
							+ key + "\" scope = \"" + scope + "\" newKey = \"" 
							+ newKey + "\" newScope = \"" + newScope
							+ "\" isDelete = \"" + isDelete
							+ "\"");
					WebContextValueInfo webContextValueInfo = new WebContextValueInfo(
							valueId, key, scope, newKey, newScope, isDelete);

					values.add(webContextValueInfo);
				}
				logger.debug("  load webcontext node id = \"" + id + "\" next id = \"" + nextId
						+ "\"");
				webContextNodeInfo.setValueInfos(values);
				webContextNodeInfos.add(webContextNodeInfo);
			}

		} catch (Exception e) {
			logger.error("load webcontext node failed!", e);
			throw new SaturnComponentException("load webcontext node failed!", e);
		}
		return webContextNodeInfos;
	}
}
